<?php
	/**
	 *  批量注册
	 */
    class BatchRegister extends APIBase {
		
		public $tag = "BatchRegister";
		public $isLogin = false;
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			return true;
		}

		public function logic() {
			
			
			if(!isset($this->param['batch'])||$this->param['batch']==''){
				$this->returnError(310,'请输入批量注册数量');
			}
			
            if((int)$this->param['batch'] < 0){
				$this->returnError(310,'输入参数有误');
			}
			$batch = $this->param['batch'];
			
			
			$this->mysql->query('delete from player where isrobot = 1');
			$this->initDataRedis(0);
			$keys = $this->data_redis->keys('hu:*');
			
			$keys_size = count($keys);
		    $keys2 = array();

	        for($e=0;$e<$keys_size;$e++){
			    if(substr_count($keys[$e],':')<2){
			    $keys2[]=$keys[$e];
			    }
		    }
			
			foreach($keys2 as $val){
				$isrobot = $this->data_redis->hget($val,'isrobot');
				if($isrobot){
					$this->data_redis->delete($val);
					$this->data_redis->delete($val.':*');
				}
			}
            
			
			for($i=1;$i<=$batch;$i++){
				$new_user = $this->createUser($i);
				$uid = $this->mysql->insert("player", $new_user);
				if ($uid == 0) {
				    $this->e("insert user error");
				    $this->returnError(500, "server is error");
			    }
			    
			    $new_user['id'] = $uid;
			    $ret = $this->data_redis->hMset("hu:{$uid}", $new_user);
			    if (!$ret) {
				    $this->e("redis hMset error.");
				    $this->returnError(3, "redis hMset error.");
			    }
			}

			$this->returnData('succuss!!');
		}
		

		private function createUser($i) {
			$now = time();
			$ip = $this->getRealIp();
			$salt = $this->get_salt(6);
			$user_info = array();
			$user_info["user"] = 'robot'.$i;
			$user_info["passport"] = '123456';
			$user_info["accid"] = rand(1,100);
			$user_info["name"] = 'robot'.$i;
			$user_info["platform_type"] = '1';
			$user_info["password"] = md5('112233' . $salt);
			$user_info["salt"] = $salt;
			$user_info["status"] = 1; // 0：为封号的
			$user_info["sex"] = 1;
			$user_info["skey"] = md5(time().$salt);
			$user_info["login_days"] = 0;
			$user_info["is_get_login_reward"] = 0;
			$user_info["heartbeat_at"] = $now;
			$user_info["create_time"] = $now;
			$user_info["update_time"] = $now;
			$user_info["avater_url"] = "";
            $user_info["is_vip"] = 0;
			$user_info["register_ip"] = $ip;
			$user_info["login_ip"] = $ip;
			//$user_info["round_card_num"]=1000;
			$user_info["point"] = 0;
			$user_info["payment"]=1;
			$user_info["device_num"] = 'abcdef';
			$user_info["isrobot"] = 1;
			return $user_info;
		}
		
		private function get_salt($len) {
			$salt = Game::$salt_password;
			return $salt;
		}
		
		public function after() {
    		$this->deinitDataRedis();
			$this->deinitCacheRedis();
			$this->deinitMysql();
		}
    }
?>
